package cn.edu.scau.info.hhb.jReflashDns;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class Loader {
	public static final String file = "ddns.conf";

	public static List<Domain> load() {
		Properties p = new Properties();
		FileInputStream in = null;

		List<Domain> lst = new ArrayList<Domain>();

		try {
			in = new FileInputStream(file);
			p.load(in);
			int count=0;
			try {
				count = Integer.parseInt(p.getProperty("count", "0"));
			} catch (Exception e) {
				return lst;
			}
			for(int i=0;i<count;i++){
				String strType=p.getProperty("type"+i,"public");
				int type =Domain.TYPE_PUBLIC;
				if(strType.equalsIgnoreCase("local")){
					type=Domain.TYPE_LOCAL;
				}
				String iface = p.getProperty("interface"+i,"");

				String orayDomain = p.getProperty("domain"+i);

				String usr = p.getProperty("orayusr"+i);

				String passwd = p.getProperty("oraypasswd"+i);
				
				String ipMatch=p.getProperty("ipmatch"+i,".*");
				String ipDismatch=p.getProperty("ipdismatch"+i,"");
				
				Domain domain = new Domain(iface,ipMatch,ipDismatch,type,usr,passwd,orayDomain);
				
				if(passwd==null || orayDomain==null || usr==null) {
					System.err.println("Configuration Error. i="+i);
					System.err.println("Error: "+domain);
					continue;
				}
				
				
				lst.add(domain);
			}
			
			return lst;

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return lst;
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

	}

}
